<?php

/**
 * Created by Allen.
 * Date: 15-10-12
 * 会员模型
 */

class User_model extends HS_Model
{


    //获取结果集
    public function getUserList($where = array(), $limit = '20', $offset = '0', $order_by = 'id desc')
    {
        return $this->select('user.*,user_level.user_level_name,user_level.color')
            ->join('user_level', 'user_level.id = user.user_level')
            ->where($where)->limit($limit, $offset)->order_by($order_by)->find_all();
    }
    //获取所有结果集
    public function getUserAll($where = array(), $order_by = 'price desc')
    {
        if (!empty($where['id in']) && is_array($where['id in'])) {
            $ids = $where['id in'];
            unset($where['id in']);
            return $this->select('user.*,user_level.user_level_name,user_level.color')
                ->join('user_level', 'user_level.id = user.user_level')->where_in('user.id', $ids)->where($where)->order_by($order_by)->find_all();
        }
        return $this->select('user.*,user_level.user_level_name,user_level.color')
            ->join('user_level', 'user_level.id = user.user_level')->where($where)->order_by($order_by)->find_all();
    }
    public function getFeildList($where, $fileds = '*', $order_by = 'score desc')
    {
        return $this->select($fileds)
            ->where($where)->order_by($order_by)->find_all();
    }
    //获取所有结果集
    public function getUserIds($where = array(), $order_by = 'id asc')
    {
        return $this->select('user.id,user.parent_id,user.price,user.parent_path,user.performance,user.score')->where($where)->find_all();
    }
    public function getNextCount($user_id = 0)
    {
        $where['parent_id'] = $user_id;
        return $this->where($where)->count();
    }
    public function getNextNextCount($user_id = 0)
    {
        $where['parent_id'] = $user_id;
        $data = $this->select('sum(`next_count`) as sum_next_count')->where($where)->find();
        if (isset($data['sum_next_count'])) {
            return $data['sum_next_count'];
        } else {
            return 0;
        }
    }
    //计算行数
    public function getCount($where = array())
    {
        return $this->where($where)->count();
    }

    //获取单个
    public function checkUser($where)
    {
        if (isset($where['id'])) {
            $where['user.id'] = $where['id'];
            unset($where['id']);
        }
        return $this->select('user.*,user_level.user_level_name,user_level.color')
            ->join('user_level', 'user_level.id = user.user_level')->where($where)->find();
    }

    //删除
    public function deleteUser($where)
    {
        return $this->where($where)->delete();
    }

    //编辑
    public function editUser($where, $data)
    {
        return $this->where($where)->edit($data);
    }
    //编辑
    public function editUserByIds($userIds, $data)
    {
        return $this->where_in('id', $userIds)->edit($data);
    }


    //添加
    public function addUser($data)
    {
        if ($this->add($data)) {
            return $this->db->insert_id();
        }
    }
    //获取所有的父级
    public function getParent($where, $pUser = array())
    {
        $user = $this->select('id,username,parent_id,user_level')->where($where)->find();
        //echo "<pre>";print_r($user);
        if (isset($user['parent_id']) && $user['parent_id']) {
            $pUser[] = $user;
            return $this->getParent(array('id' => $user['parent_id']), $pUser);
        } else {
            $pUser[] = $user;
        }
        //echo "<pre>";print_r($pUser);
        return $pUser;
    }
    //获取所有的父级
    public function getParent_id($where, $pUser = '')
    {
        $user = $this->select('id,parent_id')->where($where)->find();
        //echo "<pre>";print_r($user);
        if (isset($user['parent_id']) && $user['parent_id']) {
            $pUser .= $user['id'] . ',';
            return $this->getParent_id(array('id' => $user['parent_id']), $pUser);
        } else {
            $pUser .= $user['id'];
        }
        // echo "<pre>";print_r($pUser);
        return $pUser;
    }
    //获取所有的子级
    public function getChilden($user_ids = array(), $pUser = array())
    {
        if (isset($user_ids) && count($user_ids) > 0) {
            //echo array_pop($user_ids);
            $users = $this->select('id,username,parent_id,user_level')->where(array('parent_id' => array_pop($user_ids)))->find_all();
            // echo $this->db->last_query();
            //echo "<pre>";print_r($users);
            if (isset($users) && count($users) > 0) {
                foreach ($users as $uk => $uv) {
                    $user_ids[] = $uv['id'];
                    $pUser[] = $uv;
                }
            }
            return $this->getChilden($user_ids, $pUser);
        } else {
            return $pUser;
        }
    }
    //获取会员的关系链
    public function getChainList($id, $field = 'id,username,parent_id,user_level')
    {
        $sql = "select $field from user where  find_in_set($id,parent_path) order by LENGTH(parent_path)";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }
}
